package com.yungu.swift.schedule.config;

import com.yungu.swift.schedule.job.*;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;

/**
 * @author xiuyin.cui
 * @Description
 * @date 2019/3/11 18:05
 */

@Configuration
@Slf4j
public class QuartzConfig implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        try {
            schedulerFactoryBean.getObject().start();
            log.info("Scheduler start success...");
        } catch (SchedulerException e) {
            log.error("Scheduler start error.", e);
        }
    }

    @Bean
    public QuartzManager quartzManager() {
        QuartzManager quartzManager = new QuartzManager();
        quartzManager.setScheduler(schedulerFactoryBean.getScheduler());
        return quartzManager;
    }

    @Bean
    public SchedulerFactoryBean quartzScheduler(DataSource dataSource) {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setDataSource(dataSource);
        schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContextKey");
        schedulerFactoryBean.setConfigLocation(new ClassPathResource("quartz.properties"));
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        schedulerFactoryBean.setTriggers(
                drawGiftCountAlwaysNotSuccessOrderPassengerTrigger().getObject(),
                drawGiftCountHighFrequencyPassengerTrigger().getObject(),
                drawGiftCountSleepPassengerTrigger().getObject(),
                drawGiftSendAlwaysNotSuccessOrderPassengerGiftsTrigger().getObject(),
                drawGiftSendHighFrequencyPassengerGiftsTrigger().getObject(),
                drawGiftSendSleepPassengerGiftsTrigger().getObject(),
                driverStatisticWorkTimeTrigger().getObject(),
                joinOrderCloseOrderTrigger().getObject(),
                joinOrderDeparOrderPushMsgTrigger().getObject(),
                joinOrderRefundTrigger().getObject(),
                orderOverTimeExceptionHandlingTrigger().getObject(),
                scanDriverPointExamineTrigger().getObject(),
                scanDriverPointInsertWorkTimeTrigger().getObject(),
                scanDriverPointStatusSplitTrigger().getObject(),
                statisticsChannelPersonTrigger().getObject(),
                thawDriverFrozenBalanceTrigger().getObject(),
                heatMapTrigger().getObject(),
                wxToBankTrigger().getObject(),
                activityQuartzTrigger().getObject()
                /*demoTrigger()*/
        );
        return schedulerFactoryBean;
    }


    /*预约活动开启统计*/
    @Bean
    public JobDetailFactoryBean activityQuartzJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(ActivityQuartzJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean activityQuartzTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(activityQuartzJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 */5 * * * ?");
        return cronTriggerFactoryBean;
    }

    /*微信转账到银行卡（提现进度）*/
    @Bean
    public JobDetailFactoryBean wxToBankJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(QueryWxToBankJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean wxToBankTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(wxToBankJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0/30 * * *  ?");
        return cronTriggerFactoryBean;
    }

    /* 统计热力图数据*/
    @Bean
    public JobDetailFactoryBean heatMapJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(HeatMapJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean heatMapTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(heatMapJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 */1 * * * ?");
        return cronTriggerFactoryBean;
    }

    /* 统计高频用户*/
    @Bean
    public JobDetailFactoryBean drawGiftCountHighFrequencyPassengerJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(DrawGiftCountHighFrequencyPassengerJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean drawGiftCountHighFrequencyPassengerTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(drawGiftCountHighFrequencyPassengerJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
        return cronTriggerFactoryBean;
    }


    /* 统计沉睡用户*/
    @Bean
    public JobDetailFactoryBean drawGiftCountSleepPassengerJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(DrawGiftCountSleepPassengerJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean drawGiftCountSleepPassengerTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(drawGiftCountSleepPassengerJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
        return cronTriggerFactoryBean;
    }


    /* 发放连续叫单不成功用户礼包*/
    @Bean
    public JobDetailFactoryBean drawGiftSendAlwaysNotSuccessOrderPassengerGiftsJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(DrawGiftSendAlwaysNotSuccessOrderPassengerGiftsJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean drawGiftSendAlwaysNotSuccessOrderPassengerGiftsTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(drawGiftSendAlwaysNotSuccessOrderPassengerGiftsJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0 12 * * ?");
        return cronTriggerFactoryBean;
    }

    /* 统计连续叫单不成功用户*/
    @Bean
    public JobDetailFactoryBean drawGiftCountAlwaysNotSuccessOrderPassengerJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(DrawGiftCountAlwaysNotSuccessOrderPassengerJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean drawGiftCountAlwaysNotSuccessOrderPassengerTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(drawGiftCountAlwaysNotSuccessOrderPassengerJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
        return cronTriggerFactoryBean;
    }

    /* 发放高频用户礼包 */
    @Bean
    public JobDetailFactoryBean drawGiftSendHighFrequencyPassengerGiftsJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(DrawGiftSendHighFrequencyPassengerGiftsJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean drawGiftSendHighFrequencyPassengerGiftsTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(drawGiftSendHighFrequencyPassengerGiftsJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0 12 * * ?");
        return cronTriggerFactoryBean;
    }

    /* 发放沉睡用户礼包 */
    @Bean
    public JobDetailFactoryBean drawGiftSendSleepPassengerGiftsJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(DrawGiftSendSleepPassengerGiftsJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean drawGiftSendSleepPassengerGiftsTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(drawGiftSendSleepPassengerGiftsJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0 12 * * ?");
        return cronTriggerFactoryBean;
    }

    /* 统计司机工时 */
    @Bean
    public JobDetailFactoryBean driverStatisticWorkTimeJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(DriverStatisticWorkTimeJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean driverStatisticWorkTimeTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(driverStatisticWorkTimeJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");
        return cronTriggerFactoryBean;
    }

    /* （跨城）触发订单超时关闭  */
    @Bean
    public JobDetailFactoryBean joinOrderCloseOrderJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(JoinOrderCloseOrderJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean joinOrderCloseOrderTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(joinOrderCloseOrderJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 * * * * ?");
        return cronTriggerFactoryBean;
    }

    /* （跨城）触发订单出发前消息推送  */
    @Bean
    public JobDetailFactoryBean joinOrderDeparOrderPushMsgJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(JoinOrderDeparOrderPushMsgJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean joinOrderDeparOrderPushMsgTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(joinOrderDeparOrderPushMsgJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 * * * * ?");
        return cronTriggerFactoryBean;
    }

    /* （跨城）触发退款查询 */
    @Bean
    public JobDetailFactoryBean joinOrderRefundJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(JoinOrderRefundJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean joinOrderRefundTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(joinOrderRefundJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 */30 * * * ?");
        return cronTriggerFactoryBean;
    }

    /* 订单超时异常处理 */
    @Bean
    public JobDetailFactoryBean orderOverTimeExceptionHandlingJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(OrderOverTimeExceptionHandlingJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean orderOverTimeExceptionHandlingTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(orderOverTimeExceptionHandlingJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0 2 * * ?");
        return cronTriggerFactoryBean;
    }

    /* 司机考核 */
    @Bean
    public JobDetailFactoryBean scanDriverPointExamineJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(ScanDriverPointExamineJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean scanDriverPointExamineTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(scanDriverPointExamineJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0 2 * * ?");
        return cronTriggerFactoryBean;
    }


    /* 扫描出车司机点任务 */
    @Bean
    public JobDetailFactoryBean scanDriverPointInsertWorkTimeJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(ScanDriverPointInsertWorkTimeJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean scanDriverPointInsertWorkTimeTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(scanDriverPointInsertWorkTimeJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 * * * * ?");
        return cronTriggerFactoryBean;
    }

    /* 空驶跨天司机状态 */
    @Bean
    public JobDetailFactoryBean scanDriverPointStatusSplitJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(ScanDriverPointStatusSplitJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean scanDriverPointStatusSplitTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(scanDriverPointStatusSplitJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 1 0 * * ?");
        return cronTriggerFactoryBean;
    }

    /* 统计当天渠道邀请的用户 */
    @Bean
    public JobDetailFactoryBean statisticsChannelPersonJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(StatisticsChannelPersonJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean statisticsChannelPersonTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(statisticsChannelPersonJob().getObject());
        cronTriggerFactoryBean.setCronExpression("1 0 0 * * ?");
        return cronTriggerFactoryBean;
    }

    /* 司机解冻定时器 */
    @Bean
    public JobDetailFactoryBean thawDriverFrozenBalanceJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(ThawDriverFrozenBalanceJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    @Bean
    public CronTriggerFactoryBean thawDriverFrozenBalanceTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(thawDriverFrozenBalanceJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0 1 * *  ?");
        return cronTriggerFactoryBean;
    }

}
